package com.forlove.app.qselect.login.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.login.entity.LoginEntity;
import com.forlove.app.qselect.login.service.IValidateCodeService;
import com.forlove.app.qselect.login.service.impl.ValidateCodeServiceImpl;
import com.forlove.app.qselect.login.view.ValidateCodeView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/10/30.
 */

public class ValidateCodePresenter extends IBasePresenter {
    private IValidateCodeService validateCodeService;
    private ValidateCodeView validateCodeView;

    public ValidateCodePresenter(ValidateCodeView validateCodeView) {
        this.validateCodeView = validateCodeView;
        if (validateCodeService == null) {
            validateCodeService = new ValidateCodeServiceImpl();
            setService(validateCodeService);
        }
    }


    /**
     * 验证验证码
     */
    public void loadIdentifyCode(Map param) {
        if (param == null) {
            return;
        }
        validateCodeService.loadData(ServerManager.ACTION_REG_STEP1, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LoginEntity loginEntity = ParseHelper.getModel(result, "data", LoginEntity.class);
                if (loginEntity != null) {
                    validateCodeView.showIdentifyCodeView(loginEntity);
                }

            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (validateCodeView != null) {
                        validateCodeView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (validateCodeView != null) {
                        validateCodeView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

    public void loadSendMsg(final LoginEntity loginEntity, String sign) {
        Map<String, String> param = new HashMap<>();
        param.put("mobile", loginEntity.mobile);
        param.put("sign", sign);
        validateCodeService.loadData(ServerManager.ACTION_RESETPW_MSG, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                loginEntity.parseJSONString(ParseHelper.getString(result, "data"));
                validateCodeView.showSendMsgAgainView(loginEntity);

            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    validateCodeView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    validateCodeView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");

                }
            }
        });
    }
}
